package com.guc.gutils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @Author: guc
 * @Description: 时间格式化工具
 * @Date: 2021/11/16 14:45
 * @Version: 1.0
 */
public class DateTimeUtil {
    public static final String PATTERN_YMD = "yyyyMMdd";
    public static final String PATTERN_YMDHMS = "yyyyMMddHHmmss";
    public static final String PATTERN_Y_M_D = "yyyy-MM-dd";
    public static final String PATTERN_Y_M_D_ZH = "yyyy年MM月dd日";
    public static final String PATTERN_Y_M_D_H_M_S = "yyyy-MM-dd HH:mm:ss";
    public static final String PATTERN_Y_M_D_H_M_S_ZH = "yyyy年MM月dd日 HH时mm分ss秒";
    public static final String PATTERN_TIME_ISO = "yyyy-MM-dd'T'HH:mm:ssXXX";
    public static final String PATTERN_TIME_ISO_MILLIS = "yyyy-MM-dd'T'HH:mm:ss.SSSXXX";
    private static final SimpleDateFormat SDF = new SimpleDateFormat(PATTERN_YMDHMS);

    private DateTimeUtil() {

    }

    /**
     * 时间格式化
     *
     * @param timeMillis 时间毫秒数
     * @param pattern    时间格式
     * @return 时间字符串
     */
    public static String format(long timeMillis, String pattern) {
        if (pattern != null && pattern.length() > 0) {
            SimpleDateFormat sdf = new SimpleDateFormat(pattern);
            return sdf.format(new Date(timeMillis));
        }
        return SDF.format(new Date(timeMillis));
    }

    /**
     * 时间格式化
     *
     * @param timeMillis 时间毫秒数
     * @return 时间字符串：yyyyMMddHHmmss
     */
    public static String format(long timeMillis) {
        return format(timeMillis, null);
    }

    /**
     * @param time    时间字符串
     * @param pattern 时间格式
     * @return Date
     */
    public static Date string2Date(String time, String pattern) {
        if (time == null) {
            return null;
        }
        try {
            if (pattern == null) {
                return SDF.parse(time);
            } else {
                return new SimpleDateFormat(pattern).parse(time);
            }
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * @param time 时间字符串:yyyyMMddHHmmss
     * @return Date
     */
    public static Date string2Date(String time) {
        return string2Date(time, null);
    }

    /**
     * 日期对象
     *
     * @param date    日期对象
     * @param pattern 时间格式
     * @return 日期字符串：yyyyMMddHHmmss
     */
    public static String date2String(Date date, String pattern) {
        if (pattern == null) {
            return SDF.format(date);
        } else {
            return new SimpleDateFormat(pattern).format(date);
        }
    }

    /**
     * 日期对象
     *
     * @param date 日期对象
     * @return 日期字符串：yyyyMMddHHmmss
     */
    public static String date2String(Date date) {
        return SDF.format(date);
    }

    /**
     * 字符串格式化转换
     *
     * @param time   时间字符串
     * @param srcPtn 原格式
     * @param dstPtn 目标格式
     * @return 目标格式字符串
     */
    public static String transform(String time, String srcPtn, String dstPtn) {
        Date date = string2Date(time, srcPtn);
        if (date == null) return time;
        return date2String(date, dstPtn);
    }


}
